import Vue from "vue";
import axios from "axios";

export default {

  getList(map = {}) {
    map.storage = map.storage || '';
    return new Promise((resolve, reject) => {
      Vue.axios.get('/image', {
        params: map
      }).then(response => {
        resolve(response.data)
      })
    })
  },

  delete(id) {
    return new Promise((resolve, reject) => {
      Vue.axios.delete('/image/' + id).then(response => {
        resolve(response.data)
      })
    })
  },


  //直接传七牛云
  async upload(data) {
    const response = await Vue.axios.get("/config/storage");
    const storageType = response.data.data.name;
    console.log(storageType);
    switch (storageType) {
      case "local":
        {
          return this.uploadLocal(data);
        }
      case "qiniu":
        {
          return this.uploadQiniu(data);
        }
    }
  },

  uploadLocal(data) {
    // console.log(data);
    return new Promise((resolve, reject) => {
      data.append("storage", "local");
      Vue.axios
        .post("/image", data, {
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(response => {
          resolve(response.data);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  //直接传七牛云
  uploadQiniu(data) {
    const axiosInstance = axios.create({});
    return new Promise((resolve, reject) => {
      Vue.axios.post("/qiniu").then(response => {
        const token = response.data.data.token;
        const domain = response.data.data.domain;
        // console.log(token);

        data.append("token", token);
        //七牛云单一文件上传必须使用token,file作为key

        axiosInstance({
            method: "POST",
            url: "http://up.qiniu.com",
            data: data,
            onUploadProgress: function (progressEvent) {
              var percentCompleted = Math.round(
                (progressEvent.loaded * 100) / progressEvent.total
              );
              console.log(percentCompleted);
              //对应上传进度条
              // self.progress = percentCompleted;
            }
          })
          .then(function (res) {
            console.log("res", res);
            // let { base_url, path } = res.data;
            // //页面所用字段
            // self.previewAvatar = `${base_url}${path}?imageView2/1/w/154/h/154`;
            // //传给后台不完整
            // self.formData.avatar = `${path}`;

            // const postData = {
            //   storage: 'local',
            //   base_url: domain,
            //   path: '/' + res.data.key
            // }

            let postData = new FormData();
            postData.append("storage", "qiniu");
            postData.append("base_url", domain);
            postData.append("path", '/' + res.data.key);


            Vue.axios
              .post("/image", postData, {
                headers: {
                  "Content-Type": "multipart/form-data"
                }
              })
              .then(response => {
                resolve({
                  errno: 0,
                  errmsg: "",
                  data: {
                    base_url: domain,
                    path: '/' + res.data.key
                  }
                });
              })
              .catch(error => {
                reject(error);
              });


            // resolve({
            //   errno: 0,
            //   errmsg: "",
            //   data: {
            //     base_url: domain,
            //     path: '/' + res.data.key
            //   }
            // });
          })
          .catch(function (err) {
            console.log("err", err);
            reject(err);
          });
      });
    });
  }
};
